﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Logic;
using Meqs.Logic.Interface;

/// <summary>
/// CauseFormula 的摘要说明
/// </summary>
public class CauseFormula
{
    private Dictionary<int, IList<StuCauseCipher>> _dic = null;
    private IList<StuCauseCipher> _list = null;
    private String _schoolMark;
    private int _courseID;
    //public CauseFormula(String schoolMark)
    //{
    //    _courseID = -1;
    //    _schoolMark = schoolMark;
    //    Hashtable ht = new Hashtable();
    //    ht.Add("SchoolMark", schoolMark);
    //    _list = Container<IStuCauseCipherLogic>.Instance.FindAll(ht);
    //}

    public CauseFormula(String schoolMark, int courseID)
    {
        _dic = new Dictionary<int, IList<StuCauseCipher>>();

        // 德育公式
        _list = Container<IStuCauseCipherLogic>.Instance.FindCipherByCourse(0, schoolMark);
        _dic.Add(0, _list);

        _courseID = courseID;
        _schoolMark = schoolMark;
        _list = Container<IStuCauseCipherLogic>.Instance.FindCipherByCourse(courseID, schoolMark);
        if(!_dic.ContainsKey(courseID))
            _dic.Add(courseID, _list);
    }

    /// <summary>
    /// 学期基本分
    /// </summary>
    public double BaseScore
    { 
        get
        {
            return GetParam("BaseScore");
        }
    }

    /// <summary>
    /// 平时表现分
    /// </summary>
    public double ExpressScore
    {
        get
        {
            return GetParam("ExpressScore");
        }
    }

    /// <summary>
    /// 表现分上限
    /// </summary>
    public double MaxExpressScore
    {
        get
        {
            return GetParam("MaxExpressScore");
        }
    }

    /// <summary>
    /// 平时权重
    /// </summary>
    public double ExpressWeight
    {
        get
        {
            return GetParam("ExpressWeight");
        }
    }

    /// <summary>
    /// 期末成绩权重
    /// </summary>
    public double TermEndWeight
    {
        get
        {
            return GetParam("TermEndWeight");
        }
    }

    public double DayLimit 
    {
        get 
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", _schoolMark);
            ht.Add("KeyWord", "DayLimit");
            IList<StuCauseCipher> list = Container<IStuCauseCipherLogic>.Instance.FindAll(ht);
            foreach (StuCauseCipher cipher in list)
            {
                if (cipher.KeyWord == "DayLimit")
                    return (double)cipher.Paramer;
            }
            return 0;
        }
        set 
        {
            StuCauseCipher scc = new StuCauseCipher();
            scc.KeyWord = "DayLimit";
            scc.Paramer = value;
            scc.SchoolMark = _schoolMark;
            Container<IStuCauseCipherLogic>.Instance.UpdateByKey(scc);
        }
    }

    private void AddNewCourseFormula(int courseID)
    {
        if (_dic.ContainsKey(courseID))
        {
            _list = _dic[courseID];
        }
        else
        {
            _courseID = courseID;
            _list = Container<IStuCauseCipherLogic>.Instance.FindCipherByCourse(courseID, _schoolMark);
            _dic.Add(courseID, _list);
        }
    }

    /// <summary>
    /// 计算学分
    /// </summary>
    /// <param name="sumScore">平时各项加减累计分数</param>
    /// <param name="testScore">期末成绩</param>
    /// <returns></returns>
    public double GetCauseScore(double sumScore, double testScore)
    {
        ///（基本分+平时表现分×平时各项加减累计分数/封顶分）×平时表现权重% + 期末成绩×期末成绩权重%
        if (sumScore > MaxExpressScore)
            sumScore = MaxExpressScore;
        else if (sumScore < -MaxExpressScore)
            sumScore = -MaxExpressScore;
        return GetExpressScore(sumScore) + GetTestScore(testScore);
    }
    public double GetCauseScore(double sumScore, double testScore, int courseID)
    {
        AddNewCourseFormula(courseID);
        return GetCauseScore(sumScore, testScore);
    }

    /// <summary>
    /// 平时学分
    /// </summary>
    /// <param name="sumScore"></param>
    /// <returns></returns>
    public double GetExpressScore(double sumScore)
    { 
        //（基本分+平时表现分×平时各项加减累计分数/封顶分）×平时表现权重%
        if (sumScore > MaxExpressScore)
            sumScore = MaxExpressScore;
        else if (sumScore < -MaxExpressScore)
            sumScore = -MaxExpressScore;
        return ((BaseScore + ExpressScore * sumScore / MaxExpressScore) * ExpressWeight / 100);
    }
    public double GetExpressScore(double sumScore, int courseID)
    {
        AddNewCourseFormula(courseID);
        return GetExpressScore(sumScore);
    }

    /// <summary>
    /// 德育分数
    /// </summary>
    /// <param name="score"></param>
    /// <returns></returns>
    public double GetMoralScore(double score)
    {
        AddNewCourseFormula(0);
        if (score > MaxExpressScore)
            score = MaxExpressScore;
        else if (score <= 0 ) 
            score = 0;
       //return (BaseScore + ExpressScore * score / MaxExpressScore);
        return score > MaxExpressScore ? MaxExpressScore : score;
    }

    /// <summary>
    /// 考试学分
    /// </summary>
    /// <param name="testScore"></param>
    /// <returns></returns>
    public double GetTestScore(double testScore)
    {
        return testScore * TermEndWeight / 100;
    }
    public double GetTestScore(double testScore, int courseID)
    {
        AddNewCourseFormula(courseID);
        return GetTestScore(testScore);
    }

    private double GetParam(String str)
    {
        foreach (StuCauseCipher cipher in _list)
        {
            if (cipher.KeyWord == str)
                return (double)cipher.Paramer;
        }
        return 0;
    }
}

/// <summary>
/// 获取学分等级的工具类
/// </summary>
public class CauseLevelTool
{
    private IList<StuCauseLevel> _levelList = null;
    int _year;
    int _term;
    String _schoolMark;
    public CauseLevelTool(int schoolYear, int term, String schoolMark)
    { 
        Perm perm = HttpContext.Current.Session["UserInfo"] as Perm;

        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", perm.SchoolMark);
        ht.Add("SchoolYear", schoolYear);
        ht.Add("Term", term);
        _year = schoolYear;
        _term = term;
        _schoolMark = schoolMark;
        _levelList = Container<IStuCauseLevelLogic>.Instance.FindAll(ht);
    }

    public String GetCauseLevel(int schoolYear, int term, double score)
    {
        if (_year != schoolYear || _term != term)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", _schoolMark);
            ht.Add("SchoolYear", schoolYear);
            ht.Add("Term", term);
            _year = schoolYear;
            _term = term;
            _levelList = Container<IStuCauseLevelLogic>.Instance.FindAll(ht);
        }

        ListSort<StuCauseLevel> sort = new ListSort<StuCauseLevel>(_levelList, "Max", false);
        _levelList = sort.Sort();

        foreach (StuCauseLevel level in _levelList)
        {
            if (level.Max >= score && level.Min <= score)
                return level.Caption;
        }
        return "";
    }
}